package com.yami.shop.api.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.ComplaintSuggestions;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.vo.ComplaintSuggestionsVo;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.ComplaintSuggestionsService;
import com.yami.shop.service.UserService;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 投诉建议表 前端控制器
 * </p>
 *
 * @author jcd
 * @since 2024-08-17
 */
@RestController
@RequestMapping("/p/user/complaintSuggestions")
public class ComplaintSuggestionsController {
    @Autowired
    private ComplaintSuggestionsService complaintSuggestionsService;
    @Autowired
    private UserService userService;
    /**
     * 分页查询
     *
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<ComplaintSuggestions>> page(PageParam page, ComplaintSuggestionsVo complaintSuggestionsVo) {
        String userId = SecurityUtils.getUser().getUserId();

        LambdaQueryWrapper<ComplaintSuggestions> userFundDetailLambdaQueryWrapper = new LambdaQueryWrapper<>(complaintSuggestionsVo);

        //userFundDetailLambdaQueryWrapper.exists(StringUtils.isNotBlank(complaintSuggestionsVo.getUserName()),"(select null from tz_user u where u.user_id = e_complaint_suggestions.uid and u.nick_name like '%"+complaintSuggestionsVo.getUserName()+"%')");
        userFundDetailLambdaQueryWrapper.eq(ComplaintSuggestions::getUid,userId);

        userFundDetailLambdaQueryWrapper.orderByDesc(ComplaintSuggestions::getCreateTime);
        PageParam page1 = complaintSuggestionsService.page(page,userFundDetailLambdaQueryWrapper);

        List<ComplaintSuggestions> records = page1.getRecords();

        List<ComplaintSuggestionsVo> list1=new ArrayList<>();
        for (ComplaintSuggestions detail:records){
            ComplaintSuggestionsVo vo=new ComplaintSuggestionsVo();
            BeanUtil.copyProperties(detail,vo);

            User byId = userService.getById(detail.getUid());
            if (byId != null){
                vo.setUserName(byId.getNickName());
            }
            list1.add(vo);
        }

        page1.setRecords(list1);
        return ServerResponseEntity.success(page1);
    }


    /**
     * 通过id查询用户地址管理
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    public ServerResponseEntity<ComplaintSuggestions> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(complaintSuggestionsService.getById(id));
    }

    /**
     * 新增用户地址管理
     *
     * @return 是否新增成功
     */
    @SysLog("新增用户地址管理")
    @PostMapping
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid ComplaintSuggestions complaintSuggestions) {
        //Long userId = SecurityUtils.getSysUser().getUserId();

        complaintSuggestions.setCreateTime(new Date());
        //complaintSuggestions.setCreateBy(userId);
        complaintSuggestions.setDelFlag(0);

        return ServerResponseEntity.success(complaintSuggestionsService.save(complaintSuggestions));
    }

    /**
     * 修改用户地址管理
     *
     * @return 是否修改成功
     */
    @SysLog("修改用户地址管理")
    @PutMapping
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid ComplaintSuggestions complaintSuggestions) {
        //Long userId = SecurityUtils.getSysUser().getUserId();

        ComplaintSuggestions byId = complaintSuggestionsService.getById(complaintSuggestions.getId());
        if (byId.getState() != 0){
            throw new YamiShopBindException("已回复记录不能修改");
        }

        //complaintSuggestions.setUpdateBy(userId);
        complaintSuggestions.setUpdateTime(new Date());

        return ServerResponseEntity.success(complaintSuggestionsService.updateById(complaintSuggestions));
    }

    /**
     * 通过id删除用户地址管理
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除用户地址管理")
    @DeleteMapping("/{id}")
    public ServerResponseEntity<Boolean> removeById(@PathVariable("id") Long id) {
        ComplaintSuggestions byId = complaintSuggestionsService.getById(id);
        if (byId.getState() != 0){
            throw new YamiShopBindException("已回复记录不能删除");
        }
        return ServerResponseEntity.success(complaintSuggestionsService.removeById(id));
    }
}

